home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / NewFile / Source / Server.m < prev    next >
Text File  |  1992-11-11  |  4KB  |  175 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Server.h"
  5. #import "Protos.h"
  6. #import "Proto.h"
  7. #import "Main.h"
  8. #import <appkit/Panel.h>
  9. #import <appkit/SavePanel.h>
  10. #import <appkit/Application.h>
  11. #import <appkit/Listener.h>
  12. #import <appkit/Speaker.h>
  13. #import <appkit/Pasteboard.h>
  14. #import <appkit/Button.h>
  15. #import <appkit/PopUpList.h>
  16. #import <appkit/Matrix.h>
  17. #import <objc/List.h>
  18. #import <stdio.h>
  19. #import <strings.h>
  20. #import <sys/types.h>
  21. #import <sys/stat.h>
  22. #import <sys/param.h>
  23. #import <mach.h>
  24.  
  25. @implementation Server
  26.  
  27. + new
  28. {
  29.     self = [super new];
  30.     [NXApp loadNibSection:"Server.nib" owner:self];
  31.     savepanel = [SavePanel new];
  32.     // Add the view with the type pop-up-list and opening choice to save panel.
  33.     [auxview removeFromSuperview];
  34.     [savepanel setAccessoryView:auxview];
  35.     return self;
  36. }
  37.  
  38. // Add a type to the list.
  39. - addtype:(char *)name
  40. {
  41.     [typelist addItem:name];
  42.     return self;
  43. }
  44.  
  45. // Set up the type list from the list of proto's.
  46. - inittypes:list
  47. {
  48.     int i;
  49.     int count = [list count];
  50.     for (i=0; i<count; i++)
  51.         [self addtype:[[list objectAt:i] typename]];
  52.     return self;
  53. }
  54.  
  55. /*
  56. If path is a directory, then dir is a copy of path. Otherwise, dir contains the directory part of path.
  57. */
  58. void get_directory(char *dir, char *path)
  59. {
  60.     struct stat buf;
  61.     strcpy(dir, path);
  62.     stat(dir, &buf);
  63.     if ((buf.st_mode & S_IFDIR) == 0) *rindex(dir, '/') = '\0';
  64. }
  65.  
  66. // When service is activated, the current directory is in the pasteboard.
  67. - get_dir_from_pb:(char *)dir:(id)pb
  68. {
  69.     int length;
  70.     char *data;
  71.     const char *const *types;
  72.     int hasType, i;
  73.  
  74.     // Don't really need to check for the type, as it must always be
  75.     // present if a service was invoked.
  76.     types = [pb types];
  77.     hasType=0;
  78.     for (i=0; !hasType && types[i] ; i++) 
  79.         if (!strcmp(types[i], NXFilenamePboardType)) hasType=1;
  80.     if (hasType)
  81.     {
  82.         [pb readType:NXFilenamePboardType data:&data length:&length];
  83.         get_directory(dir, data);
  84.         return self;
  85.     }
  86.     else
  87.         return nil;
  88. }
  89.  
  90. /*
  91. Set the type of the file to be created. Sets the save panel's type and sets the type pop-up-list and open default.
  92. */
  93. - setfiletype:(char *)typename
  94. {
  95.     char *ext;
  96.     char *editor, editorstr[100];
  97.     id    proto;
  98.  
  99.     proto = [protos protoforname:typename];
  100.     [listbase setTitle:typename];
  101.     ext = [proto extension];
  102.     [savepanel setRequiredFileType:ext];
  103.     editor = [proto editor];
  104.     if (strcmp(editor, "") == 0)
  105.     {
  106.         [openineditor setTitle:""];
  107.         [openineditor setEnabled:NO];
  108.     }
  109.     else
  110.     {
  111.         sprintf(editorstr, "Open in %s", editor);
  112.         [openineditor setTitle:editorstr];
  113.         [openineditor setEnabled:YES];
  114.     }
  115.     [defaultopen selectCellWithTag:[proto defaultopen]];
  116.     return self;
  117. }
  118.  
  119. // Called when the user selects a type in the pop-up-list.
  120. - settype:sender
  121. {
  122.     char *typename = (char *)[[sender selectedCell] title];
  123.     return [self setfiletype:typename];
  124. }
  125.  
  126. // Handle service requests. The string udata is the typename..
  127. - createNew:(id)pb userData:(const char *)udata error:(char **)ermsg
  128. {
  129.     char *typename;
  130.     id    proto;
  131.     char directory[MAXPATHLEN];
  132.     int    tag;
  133.     
  134.     typename = (char *)udata;
  135.     proto = [protos protoforname:typename];
  136.     [self setfiletype:typename];
  137.     
  138.     if ([self get_dir_from_pb:directory:pb])
  139.     {
  140.         // Run the save panel.
  141.         if ([savepanel runModalForDirectory:directory file:""])
  142.         {
  143.             char fullname[MAXPATHLEN];
  144.             // Get the typename from the pop-up-list.
  145.             typename = (char *)[listbase title];
  146.             proto = [protos protoforname:typename];
  147.             
  148.             // Copy the file/directory
  149.             [main copyfile:(char *)[savepanel filename]:[main fullprotoname:fullname:[proto pathname]]];
  150.             
  151.             // Open appropriately.
  152.             tag = [[defaultopen selectedCell] tag];
  153.             if (tag == OPEN_WS)
  154.                 [main openinws:(char *)[savepanel filename]];
  155.             else if (tag == OPEN_EDITOR)
  156.                 [main openineditor:(char *)[savepanel filename]:[proto editor]];
  157.         }
  158.     }
  159.     
  160.     return self;
  161. }
  162.  
  163.  
  164. // Intercept the initialization of the listbase button to get the pop-up-list.
  165. - setListbase:anObject;
  166. {
  167.     listbase = anObject;
  168.     typelist = [listbase target];
  169.     [typelist setTarget:self];
  170.     [typelist setAction:@selector(settype:)];
  171.     return self;
  172. }
  173.  
  174. @end
  175.